extends Character


@export var active_duration: float = 5

@onready var amt_player: AnimationPlayer = $AnimationPlayer
@onready var active_amt_player: AnimationPlayer = $ActiveAnimationPlayer

var _last_active_time: int = 0


var active: bool:
    set(value):
        if active == value: return

        active = value
        if value:
            active_amt_player.play("active")
        else:
            active_amt_player.play("inactive")




func _ready() -> void:
    handle_damage_signals.receive_damage_applied.connect(
        func(info: DamageInfo) -> void:
            active = true
            _last_active_time = Time.get_ticks_msec()

            if data.health.value == 0:
                data.health.clear_modifiers()
            
            # 受击动画
            var x := info.source.global_position.x - global_position.x
            if x < 0: amt_player.play("had_damage_left")
            else: amt_player.play("had_damage_right")
    )


func _process(_delta: float) -> void:
    if not active: return

    if Time.get_ticks_msec() - _last_active_time >= active_duration * 1000:
        active = false

